/*
 * bootsecto.c
 *
 *  Created on: 21/10/2011
 *      Author: Pablo Arias & Juan Pablo De Blas
 */

#include "bootsecto.h"
#include "nipc.h"
#include "utils.h"
#include <sys/mman.h>


extern t_conexion *conexiones;


unsigned int getFatSize(char *bootSector){
	char aux[4] = {bootSector[0x24],bootSector[0x25],bootSector[0x26],bootSector[0x27]};
	return toLilEndian(aux,4);
}

unsigned int getReservedSectors(char *bootSector){
	char aux[2] = {bootSector[0xE],bootSector[0xF]};
	return toLilEndian(aux,2);
}

char getSectorPerCluster(char *bootSector){
	char aux[1] = {bootSector[0xD]};
	return toLilEndian(aux,1);
}

char getNumberOfFat(char *bootSector){
	char aux[1] = {bootSector[0x10]};
	return toLilEndian(aux,1);
}

unsigned int getRootCluster(char *bootSector){
	char aux[4] = {bootSector[0x2C],bootSector[0x2D],bootSector[0x2E],bootSector[0x2F]};
	return toLilEndian(aux,4);
}

unsigned int getSectorClusterN(unsigned int n,bootSector boot){
	unsigned int addrs = (n- boot.rootCluster) *boot.sectorPerCluster + boot.reservedSectors + (boot.fatCant*boot.fatSize);
	return addrs;
}


    bootSector getBootSector(char disk){
	bootSector boot;
	char *map;
	map = mmap(0,1,PROT_READ ,MAP_PRIVATE, disk, 0);
	boot.reservedSectors = getReservedSectors(map);
	boot.fatSize = getFatSize(map);
	boot.fatCant = getNumberOfFat(map);
	boot.sectorPerCluster = getSectorPerCluster(map);
	boot.rootCluster = getRootCluster(map);
	return boot;
}


bootSector getBootSectorSocket(){
	bootSector bootSec;
	nipcMessage msg;
	char *buffer;
	int i;

	while(conexiones[i].libre != FREE)
	i++;

	conexiones[i].libre = BUSY;

	sprintf(msg.payloadID, "%d",conexiones[i].socket);
	msg.type = 1;
	msg.payload = "0;";

	nipc_send(&msg, conexiones[i].socket);
	nipc_recv(&msg, conexiones[i].socket);

	conexiones[i].libre = FREE;

	buffer = strtok(msg.payload, "\0");

	bootSec.reservedSectors = getReservedSectors(buffer);
	bootSec.fatSize = getFatSize(buffer);
	bootSec.fatCant = getNumberOfFat(buffer);
	bootSec.sectorPerCluster = getSectorPerCluster(buffer);
	bootSec.rootCluster = getRootCluster(buffer);

	return bootSec;
}

Nodo *getFreeClusters(char disk){
	int *map;
	Nodo *free = NULL;
	int i;

	map = mmap(0,1,PROT_READ ,MAP_PRIVATE, disk, 0x4000);

	for(i = 2; i < 1026; i++){
		if(map[i] == 0){
		add_int(&free,i);
		}
	}
	return free;
}

